package com.jade.linkong.service.impl;

import com.jade.common.core.text.Convert;
import com.jade.common.utils.DateUtils;
import com.jade.common.utils.StringUtils;
import com.jade.framework.util.ShiroUtils;
import com.jade.linkong.domain.EcharDataEntry;
import com.jade.linkong.domain.LkMatterProject;
import com.jade.linkong.domain.LkProject;
import com.jade.linkong.mapper.LkMatterProjectMapper;
import com.jade.linkong.mapper.LkProjectMapper;
import com.jade.linkong.service.ILkMatterProjectService;
import com.jade.tool.generator.service.impl.GenTableServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 事项Service业务层处理
 * 
 * @author jade
 * @date 2020-07-15
 */
@Service
public class LkMatterProjectServiceImpl implements ILkMatterProjectService
{
    private static final Logger log = LoggerFactory.getLogger(GenTableServiceImpl.class);
 
    @Autowired
    private LkMatterProjectMapper lkMatterProjectMapper;
    @Autowired
    private LkProjectMapper projectMapper;
 
    /**
     * 查询事项
     * 
     * @param id 事项ID
     * @return 事项
     */
    @Override
    public LkMatterProject selectLkMatterProjectById(Long id,Long projectId)
    {
        return lkMatterProjectMapper.selectLkMatterProjectById(id,projectId);
    }
 
    /**
     * 查询事项列表
     * 
     * @param lkMatterProject 事项
     * @return 事项
     */
    @Override
    public List<LkMatterProject> selectLkMatterProjectList(LkMatterProject lkMatterProject)
    {
        return lkMatterProjectMapper.selectLkMatterProjectList(lkMatterProject);
    }
 
    /**
     * 新增事项
     * 
     * @param lkMatterProject 事项
     * @return 结果
     */
    @Override
    public int insertLkMatterProject(LkMatterProject lkMatterProject)
    {
        lkMatterProject.setCreateTime(DateUtils.getNowDate());
        return lkMatterProjectMapper.insertLkMatterProject(lkMatterProject);
    }
 
    /**
     * 修改事项
     * 
     * @param lkMatterProject 事项
     * @return 结果
     */
    @Override
    public int updateLkMatterProject(LkMatterProject lkMatterProject)
    {
        lkMatterProject.setUpdateTime(DateUtils.getNowDate());
        return lkMatterProjectMapper.updateLkMatterProject(lkMatterProject);
    }
    

    /**
     * 审核事项
     * 
     * @param lkMatterProject 事项
     * @return 结果
     */
    @Override
    public int updateStatus(LkMatterProject lkMatterProject){

        lkMatterProject.setUpdateTime(DateUtils.getNowDate());
        return lkMatterProjectMapper.updateStatus(lkMatterProject.getStatus(),lkMatterProject.getStatusRemark(),lkMatterProject.getId());
    }
 
    /**
     * 删除事项对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteLkMatterProjectByIds(String ids)
    {
        return lkMatterProjectMapper.deleteLkMatterProjectByIds(Convert.toStrArray(ids));
    }
 
    /**
     * 删除事项信息
     * 
     * @param id 事项ID
     * @return 结果
     */
    @Override
    public int deleteLkMatterProjectById(Long id)
    {
        return lkMatterProjectMapper.deleteLkMatterProjectById(id);
    }
 
    @Override
    public List<LkMatterProject> getLight(LkMatterProject lmp) {
        return lkMatterProjectMapper.getLightLkMatterProject(lmp);
    }
 
    @Override
    public List<LkMatterProject> getDistinctLight(LkMatterProject lmp) {
 
        return lkMatterProjectMapper.getDistinctLightLkMatterProject(lmp);
    }
 
    @Override
    public int saveBatch(List<LkMatterProject> list,String deleteIds, String projectId) {
        //
        if(StringUtils.isNotEmpty(deleteIds)){
            try{
                lkMatterProjectMapper.deleteByIdsAndProjectId(Convert.toStrArray(deleteIds), projectId);
            }catch (Exception e){
                log.info(e.getMessage());
                e.printStackTrace();
            }
 
        }
        return lkMatterProjectMapper.saveBatch(list);
    }

    @Override
    public List<EcharDataEntry> selectEcharData() {
        return lkMatterProjectMapper.selectEcharData();
    }

    /***
     * 首页列表
     * @Author wupujian
     * @Description
     * @Date  2020/7/29
     * @Param [lmp]
     * @return java.util.List<com.jade.linkong.domain.LkMatterProject>
     **/
    @Override
    public List<LkMatterProject> getIndexTableData(LkMatterProject lmp) {
        String roleKey = ShiroUtils.getSysUser().getRoleKey();
        if (StringUtils.isNotEmpty(roleKey) && StringUtils.equals("company", roleKey)) {

            lmp.setLeaderPhone(ShiroUtils.getSysUser().getPhonenumber());
        }
        return lkMatterProjectMapper.getIndexTableData(lmp);
    }

    /***
     * 领导more+
     * @Author wupujian
     * @Description
     * @Date  2020/7/29
     * @Param [lmp]
     * @return java.util.List<com.jade.linkong.domain.LkMatterProject>
     **/
    @Override
    public List<LkMatterProject> getMoreList(LkMatterProject lmp) {
        List<LkMatterProject> list = new ArrayList<>();
        String roleKey = ShiroUtils.getSysUser().getRoleKey();
        if (StringUtils.isNotEmpty(roleKey) && StringUtils.equals("company", roleKey)) {

            lmp.setLeaderPhone(ShiroUtils.getSysUser().getPhonenumber());
        }
        List<LkProject> lkProjects = projectMapper.selectProjectMoreList(lmp);
        if(lkProjects != null && lkProjects.size() > 0){
            for(LkProject item : lkProjects){
                LkMatterProject temp = new LkMatterProject();
                temp.setMyId(item.getProId() + "");
                temp.setProId(0);
                temp.setProjectId(item.getProId());
                temp.setProName(item.getProName());
                temp.setTransitionTime(item.getTransitionTime());
                temp.setCompanyName(item.getCompanyName());
                temp.setLeader(item.getLeader());
                temp.setLightStatus(item.getLightStatus());
                EcharDataEntry echarDataEntry = lkMatterProjectMapper.selectLightCountByProject(item.getProId());
                temp.setRemark("<img  class='imgtag' src='../../img/light_02.png'>（" + echarDataEntry.getRedLightCount() + ")  <img  class='imgtag' src='../../img/light_03.png'>（" + echarDataEntry.getYelLightCount() + ")");
                list.add(temp);
                LkMatterProject temp2 = new LkMatterProject();
                temp2.setProjectId(item.getProId());
                temp2.setLight(lmp.getLight());
                List<LkMatterProject> moreList = lkMatterProjectMapper.getMoreList(temp2);
                list.addAll(moreList);
            }
        }
        return list;
    }

    @Override
    public int updateLightStatus(LkMatterProject matterProject) {
        return lkMatterProjectMapper.updateLightStatus(matterProject);
    }

    @Override
    public Map<String, List<LkMatterProject>> selectLightGroupByOrg(LkMatterProject matterProject) {
        List<LkMatterProject> lkMatterProjects = lkMatterProjectMapper.selectLightGroupByOrg(matterProject);
        Map<String, List<LkMatterProject>> map = null;

        if(StringUtils.isNotEmpty(matterProject.getMainOrg())){
           map = lkMatterProjects.stream().collect(Collectors.groupingBy(LkMatterProject::getLight));
        }else{
            map = lkMatterProjects.stream().collect(Collectors.groupingBy(LkMatterProject::getMainOrgname));
        }
        return map;
    }

    /**
     * 查询部门事项信息
     * @return
     */
    @Override
    public Map<String,List<LkMatterProject>> selectListBydept(){
        List<LkMatterProject> matterProjectList = lkMatterProjectMapper.selectListBydept();
        Map<String, List<LkMatterProject>> map = matterProjectList.stream().collect(Collectors.groupingBy(LkMatterProject::getMainOrgname));
        return map;
    }

    @Override
    public int updateAcceptingState(Long deptId) {
        return lkMatterProjectMapper.updateAcceptingState(deptId);
    }
}
